﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfLibrary;

public class ThemeSettingsViewModel : ViewModelBase
{
    public ThemeSettingsViewModel()
    {
        CurrTheme = PaletteHelper.GetTheme();

        IsDarkTheme = CurrTheme.GetBaseTheme() == BaseTheme.Dark;

        _isColorAdjusted = CurrTheme.ColorAdjustment is not null;
        var colorAdjustment = CurrTheme.ColorAdjustment ?? new ColorAdjustment();
        _desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
        _contrastValue = colorAdjustment.Contrast;
        _colorSelectionValue = colorAdjustment.Colors;

        if (PaletteHelper.GetThemeManager() is { } themeManager)
        {
            themeManager.ThemeChanged += (_, e) =>
            {
                IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
            };
        }
    }
    PaletteHelper PaletteHelper { get; set; } = new PaletteHelper();
    Theme CurrTheme { get; set; }

    private bool _isDarkTheme;
    public bool IsDarkTheme
    {
        get => _isDarkTheme;
        set
        {
            if (SetProperty(ref _isDarkTheme, value))
            {
                CurrTheme.SetBaseTheme(value ? BaseTheme.Dark : BaseTheme.Light);
                PaletteHelper.SetTheme(CurrTheme);
            }
        }
    }

    private bool _isColorAdjusted;
    public bool IsColorAdjusted
    {
        get => _isColorAdjusted;
        set
        {
            if (SetProperty(ref _isColorAdjusted, value))
            {
                CurrTheme.ColorAdjustment = value ? new ColorAdjustment
                {
                    DesiredContrastRatio = DesiredContrastRatio,
                    Contrast = ContrastValue,
                    Colors = ColorSelectionValue
                } : null;
                PaletteHelper.SetTheme(CurrTheme);
            }
        }
    }

    private float _desiredContrastRatio = 4.5f;
    public float DesiredContrastRatio
    {
        get => _desiredContrastRatio;
        set
        {
            if (SetProperty(ref _desiredContrastRatio, value))
            {
                if (CurrTheme.ColorAdjustment != null)
                {
                    CurrTheme.ColorAdjustment.DesiredContrastRatio = value;
                    PaletteHelper.SetTheme(CurrTheme);
                }
            }
        }
    }

    public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();

    private Contrast _contrastValue;
    public Contrast ContrastValue
    {
        get => _contrastValue;
        set
        {
            if (SetProperty(ref _contrastValue, value))
            {
                if (CurrTheme.ColorAdjustment != null)
                {
                    CurrTheme.ColorAdjustment.Contrast = value;
                    PaletteHelper.SetTheme(CurrTheme);
                }
            }
        }
    }

    public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();

    private ColorSelection _colorSelectionValue;
    public ColorSelection ColorSelectionValue
    {
        get => _colorSelectionValue;
        set
        {
            if (SetProperty(ref _colorSelectionValue, value))
            {
                if (CurrTheme.ColorAdjustment != null)
                {
                    CurrTheme.ColorAdjustment.Colors = value;
                    PaletteHelper.SetTheme(CurrTheme);
                }
            }
        }
    }
}
